package com.business.action.admin.database;

import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.rose.boot.bean.DataTypeBean;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.bean.FieldBean;
import net.sf.rose.jdbc.bean.TableBean;
import net.sf.rose.jdbc.dao.MapDAO;
import net.sf.rose.jdbc.query.MapSQL;
import net.sf.rose.jdbc.service.MetaService;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.util.StringUtil;

@Controller
@RequestMapping("/admin/view")
public class ViewBeanAction {

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/list.do")
	public PageBean list(HttpServletRequest request) {
		PageBean page = new PageBean();
		page.setData(Service.getMeta().getViewList());
		return page;
	}

	/**
	 * @throws Exception 
	 * 
	 */
	@ResponseBody
	@RequestMapping("/page.do")
	public PageBean page(HttpServletRequest request, Service service, String name, String sql) throws Exception {
		PageBean page = new PageBean();
		service.setCurrentDatabaseName(name);
		MapDAO dao = new MapDAO(service);
		MapSQL query = dao.getQuerySQL();
		query.setSQL(sql);
		query.setPage(page);
		dao.list();
		List<FieldBean> fields = service.getJdbcSqlCreator().getDatabaseTable(service, name, sql);
		page.addDataMap("fields", fields);;
		return page;
	}
	
	/**
	 * 保存单个表结构信息
	 */
	@ResponseBody
	@RequestMapping("/save.do")
	public PageBean save(HttpServletRequest request, TableBean table, String json) {
		PageBean page = new PageBean();
		// 处理字段
		List<FieldBean> fields = new ArrayList<>();
		if (json != null) {
			fields = StringUtil.parseJsonArray(FieldBean.class, json);
		}
		Map<String, DataTypeBean> types = table.getDataType();
		for (FieldBean field : fields) {
			DataTypeBean type = types.get(field.getType());
			field.setJavaType(type.getJavaType());
			field.setDbType(type.getDbType());
		}
		// 处理表
		List<TableBean> list = Service.getMeta().getViewList();
		for (TableBean bean : list) {
			if (bean.getTableName().equals(table.getTableName())) {
				bean.setDatabaseName(table.getDatabaseName());
				bean.setTableName(table.getTableName());
				bean.setViewSql(table.getViewSql());
				bean.setDescription(table.getDescription());
				bean.setClassName(table.getClassName());
				bean.setFields(fields);
				return page;
			}
		}
		table.setFields(fields);
		Service.getMeta().addViewBean(table);
		return page;
	}

	/**
	 * 删除单个表结构信息
	 */
	@ResponseBody
	@RequestMapping("/delete.do")
	public PageBean delete(HttpServletRequest request, TableBean table) {
		PageBean page = new PageBean();
		List<TableBean> list = Service.getMeta().getViewList();
		for (int i = 0; i < list.size(); i++) {
			TableBean bean = list.get(i);
			if (bean.getTableName().equals(table.getTableName())) {
				list.remove(i);
				return page;
			}
		}
		return page;
	}

	/**
	 * 保存全部表结构
	 */
	@ResponseBody
	@RequestMapping("/saveAll.do")
	public PageBean saveAll(HttpServletRequest request, Service service) {
		PageBean page = new PageBean();
		MetaService ms = Service.getMeta();
		ms.saveAllTableBean();
		return page;
	}

	/**
	 * 更新数据库表结构
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping("/updateDatabase.do")
	public PageBean updateDatabase(HttpServletRequest request, Service service, TableBean table) throws Exception {
		PageBean page = new PageBean();
		service.setCurrentDatabaseName(table.getDatabaseName());
		List<TableBean> list = Service.getMeta().getViewList();
		for (TableBean bean : list) {
			if (bean.getTableName().equals(table.getTableName())) {
				String sql = bean.getViewSql();
				sql = "CREATE or REPLACE view " + bean.getTableName() + " AS " + sql;
				Statement stmt = service.getConnection().createStatement();
				stmt.execute(sql);
				System.out.println("execute sql:\n" + sql);
				stmt.close();
				service.commit();
				return page;
			}
		}
		page.setSuccess(false);
		return page;
	}
}
